<template>
  <div class="filter-box">
  <div class="wrapper">
    <div class="sch-prop sch-brand">
      <div class="sch-key">品牌：</div>
      <div class="sch-value clearfix">
        <a v-for="brand in brandData" @click="searchBrand(brand.brandId)" :key="brand.brandId">{{brand.brandName}}</a>
      </div>
    </div>
    <div class="sch-prop clearfix">
      <div class="sch-key">类别：</div>
      <div class="sch-value clearfix">
        <a v-for="category in categoryData" @click="searchCategory(category.categoryId)" :key="category.categoryId">{{category.categoryName}} </a>
      </div>
    </div>
  </div>
  </div>
</template>
<script>
import http from '../../../base/api/http'
export default {
  name: 'SearchFilterBox',
  props: {
    query: {
      type: Object,
      default: () => {
        return {
          commodityName: null,
          brandId: null,
          categoryId: null
        }
      }
    }
  },
  data () {
    return {
      brandData: [],
      categoryData: []
    }
  },
  methods: {
    getBrand () {
      http.requestQuickGet('/java/api/brand/findAll').then((res) => {
        if (res.status === 0) {
          this.brandData = res.data
        }
      })
    },
    searchBrand (val) {
      this.query.brandId = val
      this.$router.push({path: '/searchGood', query: { commodityName: this.query.commodityName, brandId: this.query.brandId, categoryId: this.query.categoryId }})
      this.$router.go(0)
    },
    searchCategory (val) {
      this.query.categoryId = val
      this.$router.push({path: '/searchGood', query: { commodityName: this.query.commodityName, brandId: this.query.brandId, categoryId: this.query.categoryId }})
      this.$router.go(0)
    },
    getCategory (id) {
      if (id) {
        http.requestGet('/java/api/category/findByBrandId', {
          'brandId': id
        }).then((res) => {
          if (res.status === 0) {
            this.categoryData = res.data
          }
        })
      } else {
        http.requestQuickGet('/java/api/category/findAll').then((res) => {
          if (res.status === 0) {
            this.categoryData = res.data
          }
        })
      }
    }
  },
  mounted () {
    this.getBrand()
    this.getCategory(this.query.brandId)
  }
}
</script>
<style>
</style>
